package entidades;

import util.MySQL;
import java.util.List;
import java.util.LinkedList;
import java.sql.SQLException;
import java.sql.ResultSet;

/**
 * @author JFM
 */
public abstract class Entidad
	{
	protected int id;



	// --- Constructores --- //

	public Entidad()
		{ this.id = 0; }

	public Entidad(int id) throws SQLException
		{
		this.setId(id);
		this.cargar();
		}

	public Entidad(String id) throws SQLException
		{
		this.setId(id);
		this.cargar();
		}

	public Entidad(ResultSet result) throws SQLException
		{ this.leer(result); }

	public Entidad(ResultSet result, String prefix) throws SQLException
		{ this.leer(result, prefix); }



	// --- Metodos get()/set() --- //

	public void setId(int id)
		{ this.id = id; }
	public void setId(String id)
		{ try{this.id = Integer.parseInt(id); }catch(Exception e) {this.id = 0;} }
	public int getId()
		{ return this.id; }

	@Override
	public String toString()
		{ return getDescriptor(); }
	
	abstract public String getDescriptor();
	abstract public String toJson();

	// --- Metodos MySQL --- //

	abstract public String getNombreTabla();
	abstract public void leer(ResultSet result, String prefijo) throws SQLException;
	abstract public void guardar() throws SQLException;

	public void leer(ResultSet result) throws SQLException
		{ this.leer(result, ""); }
		
	public void cargar() throws SQLException
		{
		ResultSet result = MySQL.executeQuery("SELECT * FROM " + this.getNombreTabla() + " WHERE id = " + this.getId());
		if(result.next()) {this.leer(result);}
		result.getStatement().close();
		}

	public void borrar() throws SQLException
		{ MySQL.executeUpdate("DELETE FROM " + this.getNombreTabla() + " WHERE id = " + this.getId()); }

	public static void borrar(String id, String nombreTabla) throws SQLException
		{ MySQL.executeUpdate("DELETE FROM " + nombreTabla + " WHERE id = " + id); }

	public static List<Entidad> listar(Class clase, String nombreTabla) throws SQLException
		{ return Entidad.buscar(clase, nombreTabla, null, null); }

	public static List<Entidad> listar(Class clase, String nombreTabla, String orderBy) throws SQLException
		{ return Entidad.buscar(clase, nombreTabla, null, orderBy); }

	public static List<Entidad> buscar(Class clase, String nombreTabla, String where) throws SQLException
		{ return Entidad.buscar(clase, nombreTabla, where, null); }

	public static List<Entidad> buscar(Class clase, String nombreTabla, String where, String orderBy) throws SQLException
		{
		LinkedList lista = new LinkedList();
		try{
			String query = "SELECT * FROM " + nombreTabla;
			if(where != null && where.length() > 0) query += " WHERE " + where;
			if(orderBy != null && orderBy.length() > 0) query += " ORDER BY " + orderBy;

			ResultSet result = MySQL.executeQuery(query);

			while(result.next())
				{
                                Entidad entidad = (Entidad) clase.newInstance();
				entidad.leer(result);
				lista.add(entidad);
				}
			result.getStatement().close();
			}catch(InstantiationException e) {}
			 catch(IllegalAccessException e) {}
		return lista;
		}



}
